package com.py.korona.ui.activities.category;

import android.app.Fragment;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.py.korona.R;
import com.py.korona.adapters.CustomListAdapterMapping;
import com.py.korona.adapters.CustomListAdapterWrapper;
import com.py.korona.db.UriBuilder;
import com.py.korona.db.model.CategoryEntity;

public class CategoryListFragment extends Fragment {
	private static final int ACTIVITY_CREATE = 2;
	private static final int ACTIVITY_EDIT = 1;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		CustomListAdapterWrapper categoriesListAdapter_ = new CustomListAdapterWrapper();
		categoriesListAdapter_.setContext(getActivity());
		categoriesListAdapter_.setMapping(new CustomListAdapterMapping() {
			@Override
			public String[] bindFromColumns() {
				return new String[] { CategoryEntity.FieldNameText };
			}

			@Override
			public int bindToLayout() {
				return R.layout.cell_category_list;
			}

			@Override
			public int[] bindToLayoutViews() {
				return new int[] { R.id.name };
			}

			@Override
			public String[] contentProperties() {
				return new String[] { CategoryEntity.FieldNameID,
						CategoryEntity.FieldNameText };
			}

			@Override
			public Uri contentURI() {
				return UriBuilder.objectListUri(CategoryEntity.uriAuthority(),
						CategoryEntity.uriBasePath());
			}

			@Override
			public ViewBinder binder() {
				return null;
			}

			@Override
			public String filter() {
				return null;
			}

			@Override
			public String[] filterArgs() {
				return null;
			}
		});
		/*
		 * init loader
		 */
		getLoaderManager().initLoader(0, null, categoriesListAdapter_);
		/*
         * 
         */
		OnItemClickListener onItemClickListener = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
				Cursor cursor = (Cursor) adapterView
						.getItemAtPosition(position);
				if (null != cursor) {
					if (cursor.moveToFirst()) {
						onItemOpenClick(id);
					}
					// do not close cursor here
				}
			}
		};
		ListView listView_ = (ListView) getActivity().findViewById(
				R.id.listView1);
		listView_.setAdapter(categoriesListAdapter_.adapter());
		listView_.setOnItemClickListener(onItemClickListener);
		this.registerForContextMenu(listView_);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.activity_category_list, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater
				.inflate(R.layout.activity_category_list_container, null);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean result;
		switch (item.getItemId()) {
		case R.id.CategoryAddMenuItem: {
			onItemAddClick();
			result = true;
			break;
		}
		default: {
			result = super.onOptionsItemSelected(item);
			break;
		}
		}
		return result;
	}

	private void onItemAddClick() {
		Intent intent = new Intent();
		intent.setClass(getActivity(), CategoryDetailActivity.class);
		intent.putExtra("readonly", false);
		startActivityForResult(intent, ACTIVITY_CREATE);
	}

	private void onItemOpenClick(long id) {
		ContentValues cv = new ContentValues();
		cv.put(CategoryEntity.FieldNameID, id);
		Uri uri = UriBuilder.objectFilterUri(CategoryEntity.uriAuthority(),
				CategoryEntity.uriBasePath(), cv);
		String contentType = UriBuilder.contentType(CategoryEntity
				.uriBasePath());
		Intent intent = new Intent(getActivity(), CategoryDetailActivity.class);
		intent.putExtra("readonly", true);
		intent.putExtra(contentType, uri);
		startActivityForResult(intent, ACTIVITY_EDIT);
	}
}
